import 'package:flutter/material.dart';

/// 星级筛选组件（下拉列表形式）
class RatingFilterWidget extends StatelessWidget {
  final int? rating; // 选中的星级，null或-1表示未选择
  final Function(int) onRatingChanged; // 星级变化回调

  const RatingFilterWidget({
    super.key,
    this.rating,
    required this.onRatingChanged,
  });

  /// 获取星级配置
  Map<String, dynamic> _getRatingConfig(int rating) {
    switch (rating) {
      case 1:
        return {
          'label': '不紧急且不重要',
          'color': const Color(0xFF9E9E9E), // 灰色
        };
      case 2:
        return {
          'label': '紧急但不重要',
          'color': const Color(0xFFFF9800), // 橙色
        };
      case 3:
        return {
          'label': '不紧急但重要',
          'color': const Color(0xFF42A5F5), // 蓝色
        };
      case 4:
        return {
          'label': '紧急且重要',
          'color': const Color(0xFFEF5350), // 红色
        };
      case 5:
        return {
          'label': '人生目标',
          'color': const Color(0xFF9C27B0), // 紫色
        };
      default:
        return {
          'label': '',
          'color': const Color(0xFFBDBDBD),
        };
    }
  }

  /// 显示下拉菜单
  void _showRatingMenu(BuildContext context) {
    final RenderBox button = context.findRenderObject() as RenderBox;
    final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject() as RenderBox;
    final RelativeRect position = RelativeRect.fromRect(
      Rect.fromPoints(
        button.localToGlobal(Offset.zero, ancestor: overlay),
        button.localToGlobal(button.size.bottomRight(Offset.zero), ancestor: overlay),
      ),
      Offset.zero & overlay.size,
    );

    showMenu<int>(
      context: context,
      position: position,
      items: [
        // 星级 > 0
        PopupMenuItem<int>(
          value: -1,
          child: Row(
            children: [
              const SizedBox(width: 8),
              Text(
                '星级 > 0',
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[800],
                ),
              ),
            ],
          ),
        ),
        // 1-5颗星选项
        ...List.generate(5, (index) {
          final starRating = index + 1;
          final config = _getRatingConfig(starRating);
          return PopupMenuItem<int>(
            value: starRating,
            child: Row(
              children: [
                // 星星图标
                ...List.generate(starRating, (_) => Icon(
                  Icons.star,
                  size: 14,
                  color: config['color'] as Color,
                )),
                const SizedBox(width: 8),
                // 文字标签
                Text(
                  config['label'] as String,
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey[800],
                  ),
                ),
              ],
            ),
          );
        }),
      ],
    ).then((value) {
      if (value != null) {
        onRatingChanged(value);
      }
    });
  }

  /// 获取显示文本
  String _getDisplayText() {
    if (rating == null) {
      return '选择星级';
    } else if (rating == -1) {
      return '星级 > 0';
    } else {
      final config = _getRatingConfig(rating!);
      return config['label'] as String;
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => _showRatingMenu(context),
      borderRadius: BorderRadius.circular(4),
      child: Padding(
        padding: const EdgeInsets.only(left: 0, right: 6, top: 4, bottom: 4),
        child: Row(
          children: [ // 改为 children，移除 mainAxisSize，让 Row 占满整行
            // 如果有选中的星级，显示星星图标
            if (rating != null && rating! > 0) ...[
              ...List.generate(rating!, (_) {
                final config = _getRatingConfig(rating!);
                return Icon(
                  Icons.star,
                  size: 12,
                  color: config['color'] as Color,
                );
              }),
              const SizedBox(width: 4),
            ],
            // 显示文本
            Text(
              _getDisplayText(),
              style: TextStyle(
                fontSize: 12,
                color: Colors.grey[700],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
